/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.ec.generatepage.web.IdxAdvAction
   Module Description   :

   Date Created      : 2010/3/11
   Original Author   : feeling.wang
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.ec.generatepage.web;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.yaodian100.core.common.dao.impl.CommonCriteria;
import com.yaodian100.core.common.entity.SimplePager;
import com.yaodian100.core.common.service.DomainService;
import com.yaodian100.core.menu.entity.Menu;
import com.yaodian100.ec.common.EcConstant;
import com.yaodian100.ec.generate.entity.GenerateAdvertise;
import com.yaodian100.ec.generate.entity.ViewPmAdvertise;
import com.yaodian100.ec.generate.service.GenerateService;

/**
 * @author Apple.Chang
 * 
 */
public class IndexAdvV5Action extends ActionSupport implements Preparable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2381254718960527101L;
	/** logger */
	private final Logger logger = LoggerFactory.getLogger(IndexAdvV5Action.class);
	private GenerateService generateService;
	private DomainService<Menu> menuService;
	private Menu advStatusMenu; // 與 getter, setter
	private Menu advIdxTypeMenu; // 與 getter, setter
	private List<GenerateAdvertise> advertises;
	private List<ViewPmAdvertise> viewPmAdvertises;
	DateFormat format = new SimpleDateFormat(EcConstant.PATTERN_TIMESTAMP);
	private SimplePager pager;

	Calendar calendar = Calendar.getInstance();
	Date today = new Date();
	String todayStr = EcConstant.PATTERN_DATE_FORMAT.format(today);
	
	private String from;

	/******* 查询IDX页面参数 ********/
	private String advNameIdxParam;
	private String startDateIdxParam;
	private String endDateIdxParam;
	private String advStatusIdxParam;
	private String advTypeIdxParam;

	/**
	 * 新增广告-行销
	 * 
	 * @return
	 * @throws Exception
	 */
	public String addIdxAdv() throws Exception {
		from = "";
		return "addIdxAdv";
	}
	public String addPclAdv() throws Exception {
		from = "PCL";
		return "addIdxAdv";
	}

	public void prepare() throws Exception {
		clearErrorsAndMessages();
		if (null == pager) {
			pager = new SimplePager();
		}
		pager.setPageRecord(20);// defaut 20

		if (null == advStatusMenu) {
			advStatusMenu = menuService.get("Generate.AdvStatus");
		}
		/*
		if (null == advIdxTypeMenu) {
			advIdxTypeMenu = menuService.get("Generate.AdvIdxTypeV5");
		}
		
		if (StringUtils.isBlank(advTypeIdxParam)) {
			advTypeIdxParam = "IDX_MAINGOLD_V2";
		}
		*/
		logger.info("prepare ok");
	}

	/**
	 * 广告列表 - 行销
	 * 
	 * @return
	 * @throws Exception
	 */
	public String searchIdx() throws Exception {
		from = "";
		advIdxTypeMenu = menuService.get("Generate.AdvIdxTypeV5");
		if (StringUtils.isBlank(advTypeIdxParam) || advTypeIdxParam.startsWith("PCL_") ) {
			advTypeIdxParam = "IDX_MAINGOLD_V2";
		}
		
		CommonCriteria crit = new CommonCriteria();
		if (StringUtils.isNotBlank(advNameIdxParam)) {
			crit.addRlike("advName", advNameIdxParam);
		}

		if (StringUtils.isNotBlank(startDateIdxParam)) {
			crit.addGe("startDate", format.parse(startDateIdxParam + " 00:00:00"));
		}

		if (StringUtils.isNotBlank(endDateIdxParam)) {
			crit.addLe("endDate", format.parse(endDateIdxParam + " 23:59:59"));
		}

		if (StringUtils.isNotBlank(advStatusIdxParam)) {
			crit.addEq("advStatus", advStatusIdxParam);
		}
		if (StringUtils.isNotBlank(advTypeIdxParam)) {
			crit.addEq("advType", advTypeIdxParam);
		}
		crit.addEq("postType", "IDX");
		int contentSize = generateService.getViewPmAdvertiseSize(crit).intValue();
		pager.setTotalSize(contentSize);

		viewPmAdvertises = generateService.getViewPmAdvertiseList(pager.getCurrentPage() * pager.getPageRecord(), pager
				.getPageRecord(), crit, new String[] { "showSeq asc", "modifyDate desc" });
		return "listIdxAdvV5";
	}
	public String searchPcl() throws Exception {
		from = "PCL";
		advIdxTypeMenu = menuService.get("Generate.AdvPclTypeV5");
		if (StringUtils.isBlank(advTypeIdxParam) || advTypeIdxParam.startsWith("IDX_") || advTypeIdxParam.startsWith("TOP_")) {
			advTypeIdxParam = "PCL_MAINGOLD_V2";
		}
		
		CommonCriteria crit = new CommonCriteria();
		if (StringUtils.isNotBlank(advNameIdxParam)) {
			crit.addRlike("advName", advNameIdxParam);
		}

		if (StringUtils.isNotBlank(startDateIdxParam)) {
			crit.addGe("startDate", format.parse(startDateIdxParam + " 00:00:00"));
		}

		if (StringUtils.isNotBlank(endDateIdxParam)) {
			crit.addLe("endDate", format.parse(endDateIdxParam + " 23:59:59"));
		}

		if (StringUtils.isNotBlank(advStatusIdxParam)) {
			crit.addEq("advStatus", advStatusIdxParam);
		}
		if (StringUtils.isNotBlank(advTypeIdxParam)) {
			crit.addEq("advType", advTypeIdxParam);
		}
		crit.addEq("postType", "IDX");
		int contentSize = generateService.getViewPmAdvertiseSize(crit).intValue();
		pager.setTotalSize(contentSize);

		viewPmAdvertises = generateService.getViewPmAdvertiseList(pager.getCurrentPage() * pager.getPageRecord(), pager
				.getPageRecord(), crit, new String[] { "showSeq asc", "modifyDate desc" });
		return "listIdxAdvV5";
	}

	public void setAdvertises(List<GenerateAdvertise> advertises) {
		this.advertises = advertises;
	}

	public void setAdvIdxTypeMenu(Menu advIdxTypeMenu) {
		this.advIdxTypeMenu = advIdxTypeMenu;
	}

	public void setAdvNameIdxParam(String advNameIdxParam) {
		this.advNameIdxParam = advNameIdxParam;
	}

	public void setAdvStatusIdxParam(String advStatusIdxParam) {
		this.advStatusIdxParam = advStatusIdxParam;
	}

	public void setAdvStatusMenu(Menu advStatusMenu) {
		this.advStatusMenu = advStatusMenu;
	}

	public void setAdvTypeIdxParam(String advTypeIdxParam) {
		this.advTypeIdxParam = advTypeIdxParam;
	}

	public void setEndDateIdxParam(String endDateIdxParam) {
		this.endDateIdxParam = endDateIdxParam;
	}

	public void setGenerateService(GenerateService generateService) {
		this.generateService = generateService;
	}

	public void setMenuService(DomainService<Menu> menuService) {
		this.menuService = menuService;
	}

	public void setPager(SimplePager pager) {
		this.pager = pager;
	}

	public void setStartDateIdxParam(String startDateIdxParam) {
		this.startDateIdxParam = startDateIdxParam;
	}

	public List<ViewPmAdvertise> getViewPmAdvertises() {
		return viewPmAdvertises;
	}

	public void setViewPmAdvertises(List<ViewPmAdvertise> viewPmAdvertises) {
		this.viewPmAdvertises = viewPmAdvertises;
	}

	public List<GenerateAdvertise> getAdvertises() {
		return advertises;
	}

	public Menu getAdvIdxTypeMenu() {
		return advIdxTypeMenu;
	}

	public String getAdvNameIdxParam() {
		return advNameIdxParam;
	}

	public String getAdvStatusIdxParam() {
		return advStatusIdxParam;
	}

	public Menu getAdvStatusMenu() {
		return advStatusMenu;
	}

	public String getAdvTypeIdxParam() {
		return advTypeIdxParam;
	}

	public String getEndDateIdxParam() {
		return endDateIdxParam;
	}

	public SimplePager getPager() {
		return pager;
	}

	public String getStartDateIdxParam() {
		return startDateIdxParam;
	}

	public String getFrom() {
		return from;
	}

	public void setFrom(String from) {
		this.from = from;
	}
}